package instance

import (
	"log"
	"net/http"
	"strconv"
	"xiaoniu-registry/internal/cache"
	"xiaoniu-registry/internal/response"
)

// ListHandler 查询某个服务下面的实例列表
func ListHandler(w http.ResponseWriter, req *http.Request) {
	serviceName := req.URL.Query().Get("serviceName")
	if serviceName == "" {
		response.BadRequest(w, "服务名不能为空")
		return
	}

	log.Printf("Querying instances for service: %s", serviceName)
	instanceList := cache.QueryInstancesByServiceName(serviceName)
	response.Success(w, instanceList)
}

// AddInstanceHandler 新增实例
func AddInstanceHandler(w http.ResponseWriter, req *http.Request) {
	serviceName := req.URL.Query().Get("serviceName")
	ip := req.URL.Query().Get("ip")
	port := req.URL.Query().Get("port")
	weightStr := req.URL.Query().Get("weight")

	// 参数验证
	if serviceName == "" || ip == "" || port == "" {
		response.BadRequest(w, "serviceName、ip、port参数不能为空")
		return
	}

	// 权重处理
	weight := 1
	if weightStr != "" {
		if parsedWeight, err := strconv.Atoi(weightStr); err == nil && parsedWeight > 0 {
			weight = parsedWeight
		} else {
			response.BadRequest(w, "权重必须是正整数")
			return
		}
	}

	log.Printf("Adding instance: service=%s, ip=%s, port=%s, weight=%d", serviceName, ip, port, weight)
	success, err := cache.AddInstance(serviceName, ip, port, weight)
	if success {
		response.SuccessWithMessage(w, "新建实例成功", nil)
	} else {
		log.Printf("Failed to add instance: %v", err)
		response.InternalError(w, err.Error())
	}
}

// OfflineInstanceHandler 手动下线实例
func OfflineInstanceHandler(w http.ResponseWriter, req *http.Request) {
	serviceName := req.URL.Query().Get("serviceName")
	ip := req.URL.Query().Get("ip")
	port := req.URL.Query().Get("port")

	// 参数验证
	if serviceName == "" || ip == "" || port == "" {
		response.BadRequest(w, "serviceName、ip、port参数不能为空")
		return
	}

	log.Printf("Manually offline instance: service=%s, ip=%s, port=%s", serviceName, ip, port)
	success, err := cache.ManualOfflineInstance(serviceName, ip, port)
	if success {
		response.SuccessWithMessage(w, "实例下线成功", nil)
	} else {
		log.Printf("Failed to offline instance: %v", err)
		response.InternalError(w, err.Error())
	}
}

// OnlineInstanceHandler 手动上线实例
func OnlineInstanceHandler(w http.ResponseWriter, req *http.Request) {
	serviceName := req.URL.Query().Get("serviceName")
	ip := req.URL.Query().Get("ip")
	port := req.URL.Query().Get("port")

	// 参数验证
	if serviceName == "" || ip == "" || port == "" {
		response.BadRequest(w, "serviceName、ip、port参数不能为空")
		return
	}

	log.Printf("Manually online instance: service=%s, ip=%s, port=%s", serviceName, ip, port)
	success, err := cache.ManualOnlineInstance(serviceName, ip, port)
	if success {
		response.SuccessWithMessage(w, "实例上线成功", nil)
	} else {
		log.Printf("Failed to online instance: %v", err)
		response.InternalError(w, err.Error())
	}
}

// DeleteInstanceHandler 删除实例
func DeleteInstanceHandler(w http.ResponseWriter, req *http.Request) {
	serviceName := req.URL.Query().Get("serviceName")
	ip := req.URL.Query().Get("ip")
	port := req.URL.Query().Get("port")

	// 参数验证
	if serviceName == "" || ip == "" || port == "" {
		response.BadRequest(w, "serviceName、ip、port参数不能为空")
		return
	}

	log.Printf("Deleting instance: service=%s, ip=%s, port=%s", serviceName, ip, port)
	success, err := cache.DeleteInstance(serviceName, ip, port)
	if success {
		response.SuccessWithMessage(w, "删除实例成功", nil)
	} else {
		log.Printf("Failed to delete instance: %v", err)
		response.InternalError(w, err.Error())
	}
}
